package com.bezdelnick.forum.dao.hibernate;

import com.bezdelnick.forum.dao.IAnswerDao;
import com.bezdelnick.forum.dao.ITypeDao;
import com.bezdelnick.forum.domain.Answer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 18.07.12
 * Time: 12:43
 * To change this template use File | Settings | File Templates.
 */
public class AnswerDaoImpl implements IAnswerDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(ITypeDao.class);

    @Autowired(required = true)
    private SessionFactory sessionFactory;

    @Override
    public List<Answer> loadAnswers() {
        Session session = null;
        List<Answer> answers = null;
        try {
            session = sessionFactory.openSession();
            answers = session.createCriteria(Answer.class).list();
        } catch (Error error) {
            answers = null;
        } finally {
            if (session.isOpen())
                session.close();
        }
        return answers;
    }

    @Override
    public Answer getAnswerById(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            Answer answer = (Answer) session.get(Answer.class, id);
            return answer;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public List<Answer> loadAnswersByQuestionId(long id) {
        Session session = null;
        List<Answer> answers;
        try {
            session = sessionFactory.openSession();
            answers = session.createCriteria(Answer.class).add(Property.forName("questionId").eq(id)).list();
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            answers = null;
        } finally {
            if (session.isOpen())
                session.close();
        }
        return answers;
    }

    @Override
    public boolean insertAnswer(Answer answer) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            session.save(answer);
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean updateAnswer(Answer answer) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            session.update(answer);
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean deleteAnswer(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            Answer answer = new Answer();
            answer.setId(id);
            session.delete(answer);
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean deleteAnswersByQuestionId(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            String hql = "delete from Answer answer where answer.questionId= :id";
            session.createQuery(hql).setLong("id", id).executeUpdate();
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }
}
